﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WikiQuizz.Common.Interfaces;
using System.Collections.ObjectModel;
using WikiQuizz.Common.Entities;
using WikiQuizz.Common.Resource;

namespace WikiQuizz.StandardImplementations
{
    public class GameManager: IGameManager
    {
        private const int CATEGORYCOUNT = 6;
        private List<CountryEntity> _Countries { get; set; }
        private List<AnswerCategoryEntity> _Answers { get; set; }
        private List<QuizzItemEntity> _QuizzItems { get; set; }
        private int _QuizzItemCounter;

        public GameManager()
        {            
            LoadData();
            SetupQuizz();
        }

        private void InitLists()
        {            
            _QuizzItems = new List<QuizzItemEntity>();
        }

        private void LoadData()
        {
            ICountryManager manager = new CountryManager();
            _Countries = manager.Countries.ToList();
        }

        private void SetupQuizz()
        {
            InitLists();
            _QuizzItemCounter = 0;

            InitializeAnswers();

            var quizzCountries = GetQuizzCountries(10, null);

            foreach (var item in quizzCountries)
            {
                var quizzItem = new QuizzItemEntity() 
                { 
                    Country = item, 
                    AnswerCategory = _Answers[GetAnswerCategory()], 
                    Alternatives = GetQuizzCountries(3, item) 
                };
                _QuizzItems.Add(quizzItem);
            }
        }

        private static int GetAnswerCategory()
        {
            Random generator = new Random(Guid.NewGuid().GetHashCode());

            return generator.Next(0, CATEGORYCOUNT);
        }

        private ICollection<CountryEntity> GetQuizzCountries(int count, CountryEntity excludedItem)
        {
            Random generator = new Random(Guid.NewGuid().GetHashCode());
            List<int> choice = new List<int>();
            List<CountryEntity> result = new List<CountryEntity>();

            while (choice.Count < count)
            {
                int nextNumber = generator.Next(0, _Countries.Count);
                if (!choice.Contains(nextNumber) && _Countries[nextNumber].IsValid && excludedItem != _Countries[nextNumber])
                {
                    choice.Add(nextNumber);
                }
            }

            foreach (var item in choice)
            {
                result.Add(_Countries[item]);
            }

            return result;
        }

        private void InitializeAnswers()
        {
            _Answers = new List<AnswerCategoryEntity>();
            _Answers.Add(new AnswerCategoryEntity(1, Strings.SENTENCE_CATEGORY_ONE));
            _Answers.Add(new AnswerCategoryEntity(2, Strings.SENTENCE_CATEGORY_TWO));
            _Answers.Add(new AnswerCategoryEntity(3, Strings.SENTENCE_CATEGORY_THREE));
            _Answers.Add(new AnswerCategoryEntity(4, Strings.SENTENCE_CATEGORY_FOUR));
            _Answers.Add(new AnswerCategoryEntity(5, Strings.SENTENCE_CATEGORY_FIVE)); 
            _Answers.Add(new AnswerCategoryEntity(6, Strings.SENTENCE_CATEGORY_SIX));
        }

        public void InitializeGame()
        {
            SetupQuizz();
        }


        public QuizzItemEntity GetNextQuizzItem()
        {
            if (_QuizzItemCounter != 9)
            {
                _QuizzItemCounter++;
            }
            else
            {
                _QuizzItemCounter = 0;
            }

            return GetActualQuizzItem();
        }


        public QuizzItemEntity GetActualQuizzItem()
        {
            return _QuizzItems[_QuizzItemCounter];
        }


        public ICollection<CountryEntity> GetShuffledValuesFromActualQuizzItem()
        {
            Random generator = new Random(Guid.NewGuid().GetHashCode());
            List<int> choice = new List<int>();
            List<CountryEntity> result = new List<CountryEntity>();

            while (choice.Count < 4)
            {
                int nextNumber = generator.Next(0, 4);
                if (!choice.Contains(nextNumber))
                {
                    choice.Add(nextNumber);
                }
            }

            foreach (var item in choice)
            {
                switch (item)
                {
                    case 0:
                        {
                            result.Add(GetActualQuizzItem().Country);
                            break;
                        }
                    case 1:
                        {
                            result.Add(GetActualQuizzItem().Alternatives.First());
                            break;
                        }
                    case 2:
                        {
                            result.Add(GetActualQuizzItem().Alternatives.Skip(1).First());
                            break;
                        }
                    case 3:
                        {
                            result.Add(GetActualQuizzItem().Alternatives.Skip(2).First());
                            break;
                        }
                    default:
                        break;
                }
                
            }

            return result;
        }
    }
}
